/*
 * UI.cpp
 *
 *  Created on: 13-Dec-2009
 *      Author: Atul
 */

#include<fstream>
#include<iostream>
#include"common.h"
#include "yADBMS.h"
#include "Heap/Heap.h"

using namespace std;
//using namespace mydatabase;

extern BufferManager *buffer;
extern int noOfBufferFrame;
extern Database *db;
//extern Heap *heap;
extern Heap2 *heap;

class UI {
private:
	Status createDB();
	Status openDB();
	void closeDB();
public:

	UI();
	void mainMenu();
	void subMenu();
};

UI::UI() {
	db = NULL;
	buffer = NULL;
}
Status UI::createDB() {
	Status s;
	string filename;
	int n;
	cout << "\nEnter Database File Name::\n";
	cin >> filename;
	cout << "\nEnter Number of pages::\n";
	cin >> n;
	db = new Database(filename.c_str(), n, s);
	buffer = new BufferManager(db, 5, s, NULL);
//	heap = new Heap();
//	heap->initDb(1);
	heap = new Heap2(-1);	//To createDB headers
	db->saveState();
	buffer->~BufferManager();//closes db file so reopened below

	db = new Database(filename.c_str(), s);

	return s;
}
Status UI::openDB() {
	string filename;
	int n;
	Status s = ok;
	if (db == NULL) {
		cout << "\nEnter Database File Name::\n";
		cin >> filename;
		db = new Database(filename.c_str(), s);
		if (s == error){
			cout << "\nError Opening Database";
			db=NULL;
		}
	}
	if (s == ok) {
		cout << "\nEnter Number of Buffer Frames::\n";
		cin >> n;
		buffer = new BufferManager(db, n, s, NULL);
		noOfBufferFrame = n;
		//initilaise heap too todo
//		if (heap != NULL)
//			heap = new Heap();
		//		heap->initDb();

	}
	return s;
}
void UI::closeDB() {
	if (buffer != NULL) {
		buffer->~BufferManager();
		buffer = NULL;
	} else if (db != NULL)
		db->~Database();
	db = NULL;

}

void UI::mainMenu() {

	char ch;
	do {
		cout << "\n1. Create Database";
		cout << "\n2. Open Database";
		cout << "\n3. Close Database";
		cout << "\n0. Exit";
		cout << "\nEnter your choice::\n";
		cin >> ch;
		switch (ch) {
		case '0':
			closeDB();
			break;
		case '1':
			if (createDB() == ok)
				cout << "\nDatabase Successfully Created";
			else
				cout << "\nError Creating Database";
			break;
		case '2':
			if (openDB() == ok) {
				subMenu();
			} else {
				cout << "\nError Opening Database";
			}
			break;
		case '3':
			closeDB();
			break;
		default:
			break;
		}
	} while (ch != '0');

}
void UI::subMenu() {
	char ch;
	do {
		cout << "\n1. SQL";
		cout << "\n2. Buffer Manager";
		cout << "\n3. Heap Manager";
		cout << "\n4. Index Manager";
		cout << "\n5. Disk Manager";
		cout << "\n0. Back To Main Menu";
		cout << "\nEnter your choice::\n";
		cin >> ch;
		switch (ch) {
		case '1':
			sql::sqlUI();
			//SQL UI
			break;
		case '2':
			BufferManagerUI::bmUI(buffer, noOfBufferFrame);
			break;
		case '3':
			//Heap UI
			HeapGUI::initHeap();
			break;
		case '4':
			BPlusTreeUI::indexui();
			//Index UI
			break;
		case '5':
			DatabaseUI::dUI(db);
			break;
		default:
			break;
		}
	} while (ch != '0');
}
int main() {
	UI u;
	u.mainMenu();
}

